package com.cmdt.zw.jms.queue;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * Created by think pad on 2017/9/16.
 */
public class AppProducer {

    private static final String url = "tcp://127.0.0.1:61616";

    private static final String queuename = "queue-test";

    public static void main(String[] args) throws JMSException
    {
        //创建连接工厂
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(url);
        //创建连接
        Connection connection = factory.createConnection();
        //启动连接
        connection.start();
        //创建会话，是否使用事物，应答模式
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建一个队列
        Destination destination = session.createQueue(queuename);
        //创建生产者
        MessageProducer messageProducer = session.createProducer(destination);

        for(int i=0;i<100;i++)
        {
            //创建消息
            TextMessage textMessage = session.createTextMessage("test"+i);
            messageProducer.send(textMessage);
            System.out.println("发送消息"+textMessage.getText());
        }
        //关闭连接
        connection.close();
    }
}
